package com.lk.theurlpro.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.theurlpro.pojo.Allurls;
import com.lk.theurlpro.service.AllurlsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author lk
 * @date 2020/2/5 0005 19:18
 */
@Controller
public class IndexController {
    @Autowired
    AllurlsService allurlsService;

    @RequestMapping("/addUrlsPage")
    public String addListUrls(Allurls u) throws Exception{
        return "addUrls";
    }

    @RequestMapping("/addUrls")
    public String addUrls(Allurls u) throws Exception{
        allurlsService.save(u);
        return "addUrls";
    }

    @RequestMapping("/deleteUrls")
    public String deleteUrls(Allurls u) throws Exception{
        allurlsService.sign(u.getU_id(),1);//放入回收站
        return "redirect:index";
    }

    @RequestMapping("/updateUrls")
    public String updateUrls(Allurls u) throws Exception{
        allurlsService.update(u);
        return "redirect:index";
    }

    @RequestMapping("/editUrls")
    public String listCategory(int u_id, Model m) throws Exception {
        Allurls c = allurlsService.get(u_id);
        m.addAttribute("c", c);
        return "editUrls";
    }

    @RequestMapping("/index")
    public String listUrls(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "40") int size) throws Exception {
        PageHelper.startPage(start, size, "date desc");
        List<Allurls> cs = allurlsService.findAll(0);// 搜索 所有正常url
        PageInfo<Allurls> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listUrls";
    }

    @RequestMapping("/searchUrls")
    public String searchUrls(Allurls a,Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "40") int size) throws Exception {
        PageHelper.startPage(start, size, "date desc");
        List<Allurls> cs = allurlsService.findByIndex(a,0);//搜索 所有带条件的，正常url
        PageInfo<Allurls> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "listUrls";
    }

}
